{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "540\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import time\n",
    "import json \n",
    "import random\n",
    "from urllib import parse\n",
    "def main(city,keyword): \n",
    "    proxie = [\n",
    "   \"134.249.156.3:82\",\n",
    "    \"1.198.72.239:9999\",\n",
    "    \"103.26.245.190:43328\"\n",
    "    ]\n",
    "    proxies = {\n",
    "               \"http\": str(random.sample(proxie, 1))\n",
    "    }\n",
    "    city = parse.quote(city)\n",
    "    keyword  = parse.quote(keyword)\n",
    "    url_start = \"https://www.lagou.com/jobs/list_\"+keyword+\"?city=\"+city+\"&cl=false&fromSearch=true&labelWords=&suginput=\" \n",
    "    url_parse = \"https://www.lagou.com/jobs/positionAjax.json?city=\"+city+\"&needAddtionalResult=false\"\n",
    "    \n",
    "    headers = { \n",
    "        'Accept': 'application/json, text/javascript, */*; q=0.01', \n",
    "        'Referer': url_start, \n",
    "        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36' \n",
    "    } \n",
    "    keyword_un = parse.unquote(keyword)\n",
    "    data = { 'first': 'true', 'pn': 1, 'kd': keyword_un }\n",
    "    s = requests.Session() \n",
    "    s.get(url_start, headers=headers, timeout=3)   # 请求首页获取cookies \n",
    "    cookie = s.cookies   # 为此次获取的cookies \n",
    "    response = s.post(url_parse, data=data, headers=headers, proxies=proxies,cookies=cookie, timeout=3)  # 获取此次文本 \n",
    "    time.sleep(5)\n",
    "    response.encoding = response.apparent_encoding \n",
    "    text = json.loads(response.text)\n",
    "   \n",
    "    info = text[\"content\"][\"positionResult\"][\"totalCount\"] \n",
    "    print(info)\n",
    "if __name__ == '__main__':\n",
    "    city = '上海'\n",
    "    keyword = '数据分析'\n",
    "    main(city,keyword)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
